<?php
class DoanphisvsController extends AppController {

	var $name = 'Doanphisvs';
	var $uses = array('Doanphisv','Namhoc');
	
	
	function index() 
	{

		$namhocs = $this->Doanphisv->find('list',array(
														'recursive'=>0,
														'fields'=>array('Namhoc.id','Namhoc.namhoc')
														,'order'=>array('Namhoc.id DESC')));
		$nhht = key($namhocs);
		$this->Doanphisv->recursive = 0;
		$this->set('doanphisvs', $this->paginate(array('Doanphisv.namhoc_id'=>$nhht)));
		$this->set(compact('namhocs'));
	}

	function laydoanphi()
	{
		$this->layout= 'ajax';
		$nhht = $this->data['Doanphisv']['namhoc_id'];
		$this->Doanphisv->recursive = 0;
		$this->set('doanphisvs', $this->paginate(array('Doanphisv.namhoc_id'=>$nhht)));
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Đoàn phí tháng này không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('doanphisv', $this->Doanphisv->read(null, $id));
	}

	function add()
	{
		if (!empty($this->data))
		{
			$this->Doanphisv->create();
			if(!isset($this->data['Doanphisv']['thang_id']))
				$this->data['Doanphisv']['thang_id']=$this->data['thang_id'];
				$this->data['Doanphisv']['sotien']=str_replace('.', '', $this->data['Doanphisv']['sotien']);
			if ($this->Doanphisv->save($this->data)) {
				$this->Session->setFlash(__('Đã thêm đoàn phí của tháng', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu, vui lòng thử lại.', true));
			}
		}
		$namhocs = $this->Doanphisv->Namhoc->find('list',array('fields'=>array('namhoc'),'order'=>array('id DESC')));
		$nhht = key($namhocs);
		$thagdaco = $this->Doanphisv->find('list',array('fields'=>array('thang_id'),'conditions'=>array('namhoc_id'=>$nhht)));
		$thangs = $this->Doanphisv->Thang->find('list',array('conditions'=>array('namhoc_id'=>$nhht,'NOT'=>array('id'=>$thagdaco)),'fields'=>array('thang')));
		$this->set(compact('thangs','namhocs'));
	}

	function edit($id = null) 
	{
		if (!$id && empty($this->data))
		{
			$this->Session->setFlash(__('Đoàn phí tháng này không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		if(!empty($this->data))
		{
			$this->data['Doanphisv']['sotien']=str_replace('.', '', $this->data['Doanphisv']['sotien']);
			if ($this->Doanphisv->save($this->data)) {
				$this->Session->setFlash(__('Đã Sửa đoàn phí của tháng', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu, vui lòng thử lại.', true));
			}
		}
		if (empty($this->data))
		{
			$this->data = $this->Doanphisv->read(null, $id);
			$this->data['Doanphisv']['sotien']=number_format($this->data['Doanphisv']['sotien'],0,',','.');
		}
		$namhocs = $this->Doanphisv->Namhoc->find('list',array('fields'=>array('namhoc'),'order'=>array('id DESC')));
		$nhht = key($namhocs);
		$thangs = $this->Doanphisv->find('list',array(
									'recursive'=>0,
									'fields'=>array('thang_id','Thang.thang'),
									'conditions'=>array('Doanphisv.namhoc_id'=>$nhht)));
		$this->set(compact('thangs','namhocs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for doanphisv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Doanphisv->delete($id)) {
			$this->Session->setFlash(__('Doanphisv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Doanphisv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function laythang()
	{
		$this->layout='ajax';
		$nhht = $this->data['Doanphisv']['namhoc_id'];
		$thangs = $this->Doanphisv->Thang->find('list',array('conditions'=>array('namhoc_id'=>$nhht),'fields'=>array('thang')));
		$this->set(compact('thangs'));
	} 
	
	function suadoanp()
	{
		if(!empty($this->data))
		{
			$namhoc  = $this->data['Doanphisv']['namhoc_id'];
			$thang  = $this->data['Doanphisv']['thang_id'];
			$sotien  = str_replace('.','', $this->data['Doanphisv']['sotien']);
			$dp = $this->Doanphisv->find('list',array('conditions'=>array('namhoc_id'=>$namhoc,'thang_id'=>$thang)));
			if (!empty($dp))
			{
				$id = key($dp);
				if ($this->Doanphisv->updateAll(array('Doanphisv.sotien'=>$sotien),array('Doanphisv.id >='=>$id)))
				{
					$this->Session->setFlash(__('Đã điều chỉnh đoàn phí', true));
					$this->redirect(array('action' => 'index'));
				}
				else
				{
					$this->Session->setFlash(__('Không thể lưu, vui lòng thử lại.', true));
				}
			}
		}
		$namhocs = $this->Namhoc->find('list',array('conditions'=>array('id >='=>$this->Session->read('namhocht_id')),'fields'=>array('namhoc')));
		$thangs = $this->Doanphisv->Thang->find('list',array('conditions'=>array('namhoc_id'=>$this->Session->read('namhocht_id')),'fields'=>array('thang')));
		$this->set(compact('namhocs','thangs'));
	}
	
}
